<?php
/**
 * Ecart
 *
 * This file is part of Ecart.
 *
 * Ecart is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Ecart is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Ecart.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @category    Ecart
 * @package     Ecart_Account
 * @copyright   Copyright 2008-2009 E-Cart LLC
 * @license     GNU Public License V3.0
 */


class Ecart_Search_Upgrade_0_1_2 extends Ecart_Core_Model_Migration_Abstract
{
    protected $_version = '0.1.2';
    protected $_info = 'install';

    public function up()
    {
        $installer = Ecart::single('install/installer');

        if (@preg_match('/\pL/u', 'a') != 1) {
            Ecart::message()->addNotice("Ecart_Search module :PCRE unicode support is turned off.\n");
        }
        if (!function_exists("mb_strstr")) {
            Ecart::message()->addNotice("For current work Ecart_Search module need http://php.net/manual/en/mbstring.installation.php");
        }

        $installer->run("

        -- DROP TABLE IF EXISTS `{$installer->getTable('search_log')}`;
        CREATE TABLE IF NOT EXISTS `{$installer->getTable('search_log')}` (
            `id` int(10) unsigned NOT NULL auto_increment,
            `visitor_id` int(10) unsigned NOT NULL,
            `query_id` int(10) unsigned NOT NULL,
            `num_results` mediumint(8) unsigned NOT NULL default '0',
            `created_at` datetime NOT NULL,
            `site_id` smallint(9) NOT NULL,
            `customer_id` int(10) unsigned default NULL,
            PRIMARY KEY  (`id`)
        ) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 ROW_FORMAT=FIXED;

        -- DROP TABLE IF EXISTS `{$installer->getTable('search_log_query')}`;
        CREATE TABLE IF NOT EXISTS `{$installer->getTable('search_log_query')}` (
            `id` int(11) NOT NULL auto_increment,
            `query` varchar(255) NOT NULL,
            `hit` int(11) NOT NULL default '0',
            PRIMARY KEY  (`id`)
        ) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

        ");

        Ecart::single('admin/menu')
            ->add('Catalog', null, 20, 'Ecart_Catalog')
            ->add('Catalog->Reports', null, 60, 'Ecart_Admin')
            ->add('Catalog->Reports->Search Queries', 'search', 10, 'Ecart_Search');

        Ecart::single('admin/acl_resource')
            ->add('admin/search', 'Search Reports')
            ->add("admin/search/delete")
            ->add("admin/search/index")
            ->add("admin/search/list");

        Ecart::single('core/page')
            ->add('search/*/*')
            ->add('search/index/*')
            ->add('search/index/index')
            ->add('search/index/result');
    }

    public function down()
    {

    }
}